package com.jiyun.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jiyun.dto.FindDoctorList;
import com.jiyun.model.information.*;
import com.jiyun.oss.AliyunOSSUtils;
import com.jiyun.result.Result;
import com.jiyun.service.IMiddleTeamDoctorService;
import com.jiyun.service.IZlDoctorService;
import com.jiyun.service.IZlTeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-09-22
 */
@RestController
@RequestMapping("/doctor/")
@Api("医生管理")
@Slf4j
public class ZlDoctorController {


    @Autowired
    private IZlDoctorService zlDoctorService;
    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;
    @Autowired
    private IZlTeamService zlTeamService;
    @Autowired
    private IMiddleTeamDoctorService middleTeamDoctorService;
    @Autowired
    private RedisTemplate redisTemplate;



    @RequestMapping("dd")

    public Result dd(@RequestHeader String token){
//        rabbitTemplate.convertAndSend("pei.lin","解散了7777777777777");
        System.out.println("token:"+ token);
        return Result.ok();
    }

    @ApiModelProperty("团队管理分页查询接口（根据名称和地址模糊查询）")
    @RequestMapping("findPage/{pn}/{ps}")
    public Result findPage(@PathVariable Integer pn,
                           @PathVariable Integer ps,
                           @RequestBody ZlDoctor doctor) {
        System.err.println("查询条件："+doctor);

//        redis拿消息
        Object o = redisTemplate.opsForValue().get("str");
        String str = (String) o;
//        删除这条数据，防止多次拿到
        redisTemplate.delete("str");
        log.info("监听到了：" + str);
        IPage<ZlDoctor> page = zlDoctorService.findPage(pn, ps, doctor);

        FindDoctorList findDoctorList = new FindDoctorList();
        findDoctorList.setDoctorIPage(page).setStr(str);
        return Result.ok(findDoctorList);
    }

    //删除
    @ApiModelProperty("删除")
    @RequestMapping("delete/{id}")
    public Result delete(@PathVariable Long id) {
//        log.info("删除id：" + id);
        if (id == null){
            return Result.fail("id不能为空");
        }
        zlDoctorService.removeById(id);
//        中间表删除
        middleTeamDoctorService.remove(new QueryWrapper<MiddleTeamDoctor>().eq("did", id));
        return Result.ok();
    }

    @ApiModelProperty("查询所有")
    @RequestMapping("findDoctor")
    public Result findDoctor(){
        return Result.ok(zlDoctorService.list());
    }

    //图片
    @ApiModelProperty("图片上传到阿里云oss")
    @PostMapping("/uufile")
    public Result upfile(MultipartFile file) throws IOException {
        String s = aliyunOSSUtils.putObject(file.getInputStream(), file.getOriginalFilename());
        return Result.ok(s);
    }


    @Transactional
    @ApiModelProperty("添加或修改服务项目管理信息")
    @RequestMapping("saveOrUpdate/{tid}")
    public Result saveOrUpdate(@RequestBody ZlDoctor zlDoctor,@PathVariable Integer tid) {
        log.info("添加或修改服务项目管理信息：" + zlDoctor);
        if (zlDoctor == null){
            return Result.fail("ZlDoctor 对象不能为空");
        }
        if (zlDoctor.getId() == null){
            zlDoctor.setStatus("1").setCreateTime(new Date());
//            log.info("添加的数据有哪些："+zlDoctor);
            zlDoctorService.saveOrUpdate(zlDoctor);
//            查出团队id，医生id，中间表添加数据
            MiddleTeamDoctor middleTeamDoctor = new MiddleTeamDoctor();
            middleTeamDoctor.setDid(zlDoctor.getId()).setTid(zlDoctor.getTid()).setStatus(2);
            middleTeamDoctorService.save(middleTeamDoctor);

        }else {
            zlDoctorService.updateById(zlDoctor);
//            拿医生id去中间表查询
            MiddleTeamDoctor did = middleTeamDoctorService.getOne(
                    new QueryWrapper<MiddleTeamDoctor>()
                    .eq("did", zlDoctor.getId())
                    .eq("tid", tid));
            log.info("修改医生信息："+did);
            if (did != null){
                did.setDid(zlDoctor.getId()).setTid(zlDoctor.getTid()).setStatus(2);
                middleTeamDoctorService.updateById(did);
            }
        }
        return Result.ok();
    }
    @ApiModelProperty("根据ID查询")
    @RequestMapping("get/{id}")
    public ZlDoctor getById(@PathVariable Integer id) {
        return zlDoctorService.getById(id);
    }

//    接收团队解散的消息,存入redis中
    @ApiModelProperty("接收团队解散的消息")
    @RabbitListener(queuesToDeclare = @Queue("pei.lin"))
    public void jsTeam(String str) {
        log.info("接收到消息：" + str);
        redisTemplate.opsForValue().set("str", str);
    }


}
